package com.woniu.gf.pawn.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.woniu.gf.pawn.comm.core.domain.Image;
import com.woniu.gf.pawn.comm.core.domain.ProductSmallType;
import com.woniu.gf.pawn.comm.core.dto.ResultDto;
import com.woniu.gf.pawn.product.parma.*;
import com.woniu.gf.pawn.service.IProductSmallTypeService;
import com.woniu.gf.pawn.utils.BindingResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/product-small-type")
@Api(tags = "小类的控制类")
public class ProductSmallTypeController {

    @Autowired
    private IProductSmallTypeService productSmallTypeService;

    /**
     * 查找展示
     * @param findProductSmallTypePageParam
     * @return
     */
    @ApiOperation("小类查询")
   @GetMapping("findPage")
    public ResultDto findPage(@Valid FindProductSmallTypePageParam findProductSmallTypePageParam, BindingResult result){
       String bindingResult = BindingResultUtils.getBindingResult(result);
       if(bindingResult.isEmpty()){
           IPage<ShowProductSmallTypeParam> showProductSmallTypeParamIPage = productSmallTypeService.selectPageVo(findProductSmallTypePageParam);
           return ResultDto.success(200,"查询小类",showProductSmallTypeParamIPage);
       }
       return ResultDto.fail(202,"查询小类",bindingResult);
   }

    /**
     * 添加
     * @param addProductSmallTypeParam
     * @return
     */
    @ApiOperation("小类添加")
    @PostMapping("addSmallType")
    public ResultDto addSmallType(@Valid @RequestBody AddProductSmallTypeParam addProductSmallTypeParam, BindingResult result){
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()) {
            ProductSmallType productSmallType = new ProductSmallType();
            Image image = new Image();
            BeanUtils.copyProperties(addProductSmallTypeParam, image);
            BeanUtils.copyProperties(addProductSmallTypeParam, productSmallType);
            productSmallTypeService.addSmallType(productSmallType, image);
            return ResultDto.success(200, "新增成功");
        }
        return ResultDto.fail(202, "新增失败",bindingResult);
    }

    /**
     * 修改
     * @param updateProductSmallTypeParam
     * @return
     */
    @ApiOperation("小类修改")
    @PostMapping("updateSmallType")
    public ResultDto updateSmallType(@Valid @RequestBody UpdateProductSmallTypeParam updateProductSmallTypeParam, BindingResult result){
        String bindingResult = BindingResultUtils.getBindingResult(result);
        if(bindingResult.isEmpty()) {
            ProductSmallType productSmallType = new ProductSmallType();
            Image image = new Image();
            BeanUtils.copyProperties(updateProductSmallTypeParam, image);
            BeanUtils.copyProperties(updateProductSmallTypeParam, productSmallType);
            productSmallTypeService.updateSmallType(productSmallType, image);
            return ResultDto.success(200, "修改成功");
        }
        return ResultDto.fail(202, "修改失败",bindingResult);
    }

    /**
     * 删除
     * @param listIdParam
     * @return
     */
    @ApiOperation("小类删除")
    @PostMapping("deleteSmallType")
    public ResultDto deleteSmallType(@RequestBody ListIdParam listIdParam){
        if(listIdParam.getIds()==null)  return  ResultDto.fail(202,"删除失败","参数为空");
            productSmallTypeService.deleteSmallType(listIdParam.getIds());
            return ResultDto.success(200,"删除成功");

    }
}

